package cn.sxt.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.sxt.logistics.pojo.MsgObject;
import cn.sxt.logistics.pojo.Permission;
import cn.sxt.logistics.pojo.PermissionExample;
import cn.sxt.logistics.pojo.PermissionExample.Criteria;
import cn.sxt.logistics.pojo.Role;
import cn.sxt.logistics.pojo.RoleExample;
import cn.sxt.logistics.service.PermissionService;
import cn.sxt.logistics.service.RoleService;

@Controller
@RequestMapping("/permission")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;


	@RequestMapping("/permissionPage")
	public String permissionPage() {

		return "permissionPage";
	}

	// 编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m,Long permissionId) {
		
		
		//修改操作
		if(permissionId !=null) {
			//查询出当前用户的数据，共享给编辑页面，以供修改的回显数据
			Permission permission = permissionService.selectByPrimaryKey(permissionId);
			m.addAttribute("permission", permission);
		}
		
		//查询所有权限的父权限（type=menu的权限就是父权限）
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andTypeEqualTo("menu");
		List<Permission> parents = permissionService.selectByExample(example );
		m.addAttribute("parents", parents);
		
		return "permissionEdit";
	}
	
	
	@RequestMapping("/getAllPermission")
	@ResponseBody
	public List<Permission> getAllPermission(){
		PermissionExample example = new PermissionExample();
		List<Permission> permissions = permissionService.selectByExample(example );
		return permissions;
	}
	

	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<Permission> list(@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "10") Integer pageSize, String keyword) {
		System.out.println("keyword :" + keyword);
		// 开始分页
		PageHelper.startPage(pageNum, pageSize);

		PermissionExample example = new PermissionExample();

		// 关键字不为空才做条件查询
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
			Criteria criteria1 = example.createCriteria();
			criteria1.andNameLike("%" + keyword + "%");
			
			
			Criteria criteria2 = example.createCriteria();
			criteria2.andParentNameLike("%" + keyword + "%");
			
			example.or(criteria2);
		}

		List<Permission> permissions = permissionService.selectByExample(example);

		// 创建分页信息对象
		return new PageInfo<Permission>(permissions);

	}

	// 删除功能
	@RequestMapping("/insert")
	@ResponseBody
	public MsgObject insert(Permission permission) {
		
		int row = permissionService.insert(permission);
		MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！");
		if (row == 1) {
			mo = MsgObject.createMO(1, "新增数据成功!");
		}
		return mo;
	}
	
	
	// 删除功能
		@RequestMapping("/update")
		@ResponseBody
		public MsgObject update(Permission permission) {
			
			int row = permissionService.updateByPrimaryKeySelective(permission);
			MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！");
			if (row == 1) {
				mo = MsgObject.createMO(1, "修改数据成功!");
			}
			return mo;
		}
	

	// 删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long permissionId) {
		
		/*
		 * 删除之前检查对应的权限id是否还有子权限
		 * 
		 * 查询 parentId = 要删除的权限id  条件
		 */
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(permissionId);
		
		List<Permission> children = permissionService.selectByExample(example);
		
		
		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");
		
		if(children.size() > 0) {	
			mo = MsgObject.createMO(0, "此权限还有子权限，不能删除！");
			return mo;
		}
		
		int row = permissionService.deleteByPrimaryKey(permissionId);
		if (row == 1) {
			mo = MsgObject.createMO(1, "删除数据成功!");
		}
		return mo;
	}


}
